package com.quanquan.management.request;

import com.quanquan.management.dto.UserDTO;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * @description:
 * @author:lihonglinag
 * @date:2019-03-21 18:03
 * @version:v 1.0.0
 */
public class BaseRequest<T> implements Serializable {

    private Class<T> beanClass;

    public BaseRequest() {
        Class<?> clazz = this.getClass();
        Type superclass = clazz.getGenericSuperclass();
        if (superclass  instanceof ParameterizedType) {
            Type type = ((ParameterizedType)superclass).getActualTypeArguments()[0];
            this.beanClass = (Class<T>)type;
        }
    }



    public T newInstance() {
        T tInstance;
        Enhancer enHancer = new Enhancer();
        enHancer.setSuperclass(this.beanClass);
        enHancer.setCallback((MethodInterceptor) (obj, method, params, proxy) -> {
            Object result = proxy.invokeSuper(obj, params);
            if (result instanceof String) {
                result = result+"... i am after enHance";
            }
            return result;
        });
        tInstance = (T)enHancer.create();
        if (tInstance == null) {
            throw new RuntimeException("proxy对象创建失败，请重试...");
        }
        return tInstance;
    }


}
